package org.soulraven.games.memory.util;

import java.util.HashMap;
import java.util.Map;

import javax.swing.ImageIcon;

import org.apache.log4j.Logger;

public final class ImageUtil {

	private static final Logger LOGGER = Logger.getLogger(ImageUtil.class);
	private static final Map<String, ImageIcon> IMAGES = new HashMap<String, ImageIcon>();
	private static final String DECK = "DECK";
	private static final String PENGUINS_SET = "penguins/";
	private static final String BASE_DIR = System.getProperty("user.dir") + "/src/main/resources/images/";
	private static final String GIF_EXTENSION = "gif";
	private static final String PNG_EXTENSION = "png";

	private ImageUtil() {
	}

	public static final ImageIcon getDeckImage() {
		return getImage(PENGUINS_SET, DECK);
	}

	public static final ImageIcon getFavicon() {
		return getImage("", "icon", PNG_EXTENSION);
	}

	public static final ImageIcon getImage(final int number) {
		return getImage(PENGUINS_SET, String.valueOf(number));
	}

	private static final ImageIcon getImage(final String set, final String key, final String extension) {
		if (IMAGES.containsKey(key)) {
			return IMAGES.get(key);
		}
		String imageName = BASE_DIR + set + key + "." + extension;
		LOGGER.debug("Loading image " + imageName);
		ImageIcon icon = new ImageIcon(imageName);
		IMAGES.put(key, icon);
		return icon;
	}

	private static final ImageIcon getImage(final String set, final String key) {
		return getImage(set, key, GIF_EXTENSION);
	}
}
